﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;

namespace FluentFTP.Model
{
    [Serializable]
    public class FtpServerInfo : INotifyPropertyChanged, IDataErrorInfo
    {
        #region Properties

        public Guid Id { get; set; }

        private string name;
        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
                OnPropertyChanged();
            }
        }

        private string adress;
        public string Adress
        {
            get
            {
                return adress;
            }
            set
            {
                adress = value;
                OnPropertyChanged();
            }
        }

        private string userName;
        public string UserName
        {
            get
            {
                return userName;
            }
            set
            {
                userName = value;
                OnPropertyChanged();
            }
        }

        private string password;
        public string Password
        {
            get
            {
                return password;
            }
            set
            {
                password = value;
                OnPropertyChanged();
            }
        }

        #endregion Properties

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        private void OnPropertyChanged()
        {
            if (PropertyChanged != null)
            {
                var action = PropertyChanged;
                action.Invoke(this, new PropertyChangedEventArgs(string.Empty));
            }
        }

        public FtpServerInfo CreateDeepCopy()
        {
            return new FtpServerInfo { Id = Id, Name = Name, Adress = Adress, UserName = UserName, Password = Password };
        }

        /// <summary>
        /// Does not copy the 'Id' property
        /// </summary>
        /// <param name="server">FtpsSrverInfo to which the properties will be copied.</param>
        public void CopyTo(FtpServerInfo server)
        {
            server.Name = Name;
            server.Adress = Adress;
            server.UserName = UserName;
            server.Password = Password;
        }

        #region IDataErrorInfo Members

        public string Error
        {
            get
            {
                if (this["Name"] == string.Empty && this["Adress"] == string.Empty && this["UserName"] == string.Empty) return string.Empty;
                return "Invalid values";
            }
        }

        public string this[string columnName]
        {
            get
            {
                switch (columnName)
                {
                    case "Name":
                        if (string.IsNullOrEmpty(Name)) return "Name cannot be empty";
                        break;
                    case "Adress":
                        if (string.IsNullOrEmpty(Adress) || !Adress.StartsWith("ftp://", StringComparison.InvariantCultureIgnoreCase)) return "Invalid value";
                        break;
                    case "UserName":
                        if (string.IsNullOrEmpty(UserName)) return "UserName cannot be empty";
                        break;
                    default:
                        break;
                }

                return string.Empty;
            }
        }

        #endregion
    }
}
